/**
 * 坐标controller
 */
'use strict';

var _ = require('underscore');
var async = require("async");
var response = require('./response');
var config = require('../conf/config');
var db = require('../conf/db.js');
var errorCode = config.errorCode;


exports.queryAll = function(req, res){
    var user = req.session.user;
    var Coordinate = db.model(user.tenant, 'Coordinate');

    Coordinate.find({}).exec(function (err, coordinates) {
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            response.data(res, coordinates);
        }
    });
};

exports.update = function(req, res){
    var user = req.session.user;
    var Coordinate = db.model(user.tenant, 'Coordinate');
    var id = req.body.id;

    Timetable.update({_id : id}, {$set : {local : req.body.local, 
            scope : req.body.scope, 
            longitude : req.body.longitude, 
            latitude : req.body.latitude}}, function(err, count){
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            response.success(res);
        }
    });
};

exports.updateStatus = function(req, res){
    var user = req.session.user;
    var Coordinate = db.model(user.tenant, 'Coordinate');
    var id = req.params.id;
    var isEnabled = req.params.isEnabled;

    Coordinate.update({_id : id}, {$set : {isEnabled : isEnabled}}, function(err, count){
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            response.success(res);
        }
    });
};

exports.create = function(req, res){
    var user = req.session.user;
    var Coordinate = db.model(user.tenant, 'Coordinate');
    var coordinate = new Coordinate(req.body);
    coordinate.createUser = user.id;
    
    coordinate.save(function(err, coordinate){
        if (err) {
            console.error('异常信息为：%s', JSON.stringify(err));
            response.error(res, config.errorCode.databaseError);
        } else {
            response.data(res, coordinate);
        }
    });
};


